package com.cheng.imagerelated.preview;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.cheng.imagerelated.R;
import com.cheng.imagerelated.photoselecview.PhotoSelector;
import com.cheng.imagerelated.photoselecview.utils.StatusBarUtils;
import com.cheng.imagerelated.preview.adpter.PreviewImageAdapter;

import java.util.ArrayList;

public class RvPreviewActivity extends Activity {

    // view
    private RelativeLayout rlToolBarColor;
    private ImageView ivBack;

    // 需要预览的图片集合
    private ArrayList<String> images;
    // toolBar的颜色值
    private int toolBarColor;
    // bottomBar的颜色值
    private int bottomBarColor;
    // 状态栏的颜色值
    private int statusBarColor;
    // 状态栏字体是否为黑色
    private boolean isBack;

    // 预览图片数据源

    // ViewPager
    private PreviewImageAdapter previewImageAdapter;
    private RecyclerView recyclerPreview;
    private LinearLayoutManager linearLayoutManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rv_preview);

        initIntent();
    }

    private void initIntent() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        assert bundle != null;
        images = bundle.getStringArrayList(PhotoSelector.EXTRA_SELECTED_IMAGES);
        toolBarColor = bundle.getInt(PhotoSelector.EXTRA_TOOLBARCOLOR, ContextCompat.getColor(this, R.color.blue));
        bottomBarColor = bundle.getInt(PhotoSelector.EXTRA_BOTTOMBARCOLOR, ContextCompat.getColor(this, R.color.blue));
        statusBarColor = bundle.getInt(PhotoSelector.EXTRA_STATUSBARCOLOR, ContextCompat.getColor(this, R.color.blue));
        isBack = bundle.getBoolean(PhotoSelector.EXTRA_STATUSBARTEXTCOLOR, false);
        // 状态栏设置
        StatusBarUtils.setColor(this, toolBarColor, isBack);

        initView();
    }

    private void initView() {
        rlToolBarColor = findViewById(R.id.rl_tool_bar_color);
        rlToolBarColor.setBackgroundColor(toolBarColor);
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 返回
                setGoBack();
            }
        });

        initViewPager();
    }

    private void initViewPager() {
        recyclerPreview = findViewById(R.id.recycler_preview);
        recyclerPreview.setLayoutManager(new LinearLayoutManager(
                this, LinearLayoutManager.HORIZONTAL, false
        ));
        linearLayoutManager = (LinearLayoutManager) recyclerPreview.getLayoutManager();
        previewImageAdapter = new PreviewImageAdapter(this, images);
        recyclerPreview.setAdapter(previewImageAdapter);
        PagerSnapHelper snapHelper = new PagerSnapHelper();
        snapHelper.attachToRecyclerView(recyclerPreview);

        recyclerPreview.setItemAnimator(new DefaultItemAnimator());
        recyclerPreview.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    int position = linearLayoutManager.findFirstVisibleItemPosition();
                    changeSelect(images.get(position));
                }
            }
        });

    }

    private void changeSelect(String s) {

    }

    /**
     * 返回设置
     */
    private void setGoBack() {
        finish();
    }
}
